/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import rentcalculator.model.Bill;
import rentcalculator.model.Facility;
import rentcalculator.model.Lodgement;
import rentcalculator.model.Person;
import rentcalculator.model.Property;

/**
 *
 * @author K.E.Y
 */
public class RentCalculator {

    private static Property p;
    private static List<Facility> fs;
    private static Person person ;
    private static List<Lodgement> ls ;

   private static  List<Bill> bs ;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        init();
    }
    
    public static void init(){
        cfg  = new Configuration();
        cfg.configure("resources/hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
        sf = cfg.buildSessionFactory(serviceRegistry);

        Session session = sf.openSession();
        session.beginTransaction();


        
        createTestData(session);
        
        
        

        session.getTransaction().commit();
        session.close();
        sf.close();
    }
    
   
    private static void createTestData(Session s){
        p = createProperty(s);
        fs = createFacility(s, p);
        person = createPerson(s);
//        ls = createLodgeInfo(s, p, person);
//        
//        bs = createBills(s, fs.get(0));
    }
    
    private static Property createProperty(Session s){
        Property p = (Property)s.get(Property.class, (long)1);
        if(p == null){
            p = new Property();
        }
        s.save(p);
        return p;
    }
    
    private static List<Facility> createFacility(Session s, Property p){
        List<Facility> l = s.createQuery("select f.* from Facility f where name in ('water','electricity','gas','network')").list();
        
        
        if(l.size() == 0){
            Facility water = new Facility("water", p); 
            Facility gas = new Facility("gas", p); 
            Facility electricity = new Facility("electricity", p); 
            Facility network = new Facility("network", p); 
            l.add(water);
            l.add(gas);
            l.add(electricity);
            l.add(network);
           s.save(water);
           s.save(gas);
           s.save(electricity);
           s.save(network);
            
        }
        
        return l;
    }
    
    
    private static Person createPerson(Session s){
        List<Person> ps = (List<Person>)s.createQuery("select p from Person p where name in ('Mike')").list();
        Person p;
        if(ps.size() == 0){
            p = new Person("Mike");
            ps.add(p);
            s.save(p);
        }else{
            p = ps.get(0);
        }
        
        return p;
    }
    
    
    private static List<Lodgement> createLodgeInfo(Session s, Property property, Person person){
        //List<Lodgement> ls = s.createQuery("select * from Lodgement where person_id = ")
    
        return null;
    }
    
    private static List<Bill> createBills(Session s, Facility f){
        
        return null;
    }
    
    
    
    
    
    
    private static Configuration cfg;
    private static SessionFactory sf;
     
     
}
